package com.example.demo.web.common.model;

import java.io.Serializable;
import lombok.Data;
import org.noear.snack.ONode;

/**
 * @author airhead
 */
@Data
public class Ret<T> implements Serializable {
  public Integer code;
  private T data;
  private String msg;

  public static <T> Ret<T> ok() {
    return ok(null, null);
  }

  public static <T> Ret<T> ok(T data) {
    return ok(data, null);
  }

  public static <T> Ret<T> ok(T data, String msg) {
    Ret<T> result = new Ret<>();
    result.code = 0;
    result.data = data;
    result.msg = msg;
    return result;
  }

  public static <T> Ret<T> fail(String msg) {
    return fail(null, msg);
  }

  public static <T> Ret<T> fail(T data, String msg) {
    return fail(500, data, msg);
  }

  public static <T> Ret<T> fail(Integer code, T data, String msg) {
    Ret<T> result = new Ret<>();
    result.code = code;
    result.data = data;
    result.msg = msg;
    return result;
  }

  public String toJson() {
    return ONode.stringify(this);
  }
}
